package com.yugao.fintech.antelope.base.datasource.core.handler;

import com.baomidou.mybatisplus.core.handlers.MetaObjectHandler;
import org.apache.ibatis.reflection.MetaObject;
import org.springframework.stereotype.Component;

import java.util.Date;

/**
 * mybatis-plus的高级操作
 */
@Component
public class OperationTimeFillHandler implements EntityFieldFillHandler {
    @Override
    public void insertFill(MetaObject metaObject, MetaObjectHandler metaObjectHandler) {
        if (metaObject.hasSetter("updateTime")) {
            metaObject.setValue("updateTime", null);
            metaObjectHandler.strictInsertFill(metaObject, "updateTime", Date.class, new Date());
        }

        if (metaObject.hasSetter("updatedAt")) {
            metaObject.setValue("updatedAt", null);
            metaObjectHandler.strictInsertFill(metaObject, "updatedAt", Date.class, new Date());
        }

        if (metaObject.hasSetter("createTime")) {
            metaObject.setValue("createTime", null);
            metaObjectHandler.strictInsertFill(metaObject, "createTime", Date.class, new Date());
        }

        if (metaObject.hasSetter("createdAt")) {
            metaObject.setValue("createdAt", null);
            metaObjectHandler.strictInsertFill(metaObject, "createdAt", Date.class, new Date());
        }
    }

    @Override
    public void updateFill(MetaObject metaObject, MetaObjectHandler metaObjectHandler) {
        if (metaObject.hasSetter("updateTime")) {
            metaObjectHandler.strictUpdateFill(metaObject, "updateTime", Date.class, new Date());
        }
        if (metaObject.hasSetter("updatedAt")) {
            metaObject.setValue("updatedAt", null);
            metaObjectHandler.strictUpdateFill(metaObject, "updatedAt", Date.class, new Date());
        }
    }
}
